package indi.readbook.example.admin.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import indi.readbook.example.admin.service.TokenService;
import indi.readbook.example.common.entity.system.UserDO;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *    使用用户信息获取token
 * </p>
 *
 * @author zhangguowei
 * @since 2021-9-7
 **/
@Service
public class TokenServiceImpl implements TokenService {

    @Override
    public String getToken(UserDO user) {
        //设置过期时间30个小时  108000000
        Date expiresDate = new Date(System.currentTimeMillis() + 108000000);
        String token="";
        token= JWT.create()
                .withExpiresAt(expiresDate)
                .withAudience(String.valueOf(user.getUserId()))
                .sign(Algorithm.HMAC256(user.getPassword()));
        return token;
    }
}
